//
//  CXHSplashViewController.m
//  CXHAdSDK-Code_Example
//
//  Created by 陈坤 on 2022/9/28.
//  Copyright © 2022 华资融创. All rights reserved.
//

#import "CXHSplashViewController.h"
#import "CXHAdSDK-Core/CXHAdSDKSplashAd.h"
#if __has_include(<CXHAdSDK-Kit/CXHUtilsKit.h>)
#import <CXHAdSDK-Kit/CXHUtilsKit.h>
#else
#import "CXHUtilsKit.h"
#endif

@interface CXHSplashViewController ()<CXHAdSDKSplashAdDelegate,UITextFieldDelegate>

@property (nonatomic, strong) CXHAdSDKSplashAd *splashAd;

@property (nonatomic, strong) UITextField *textField;

@property (nonatomic, strong) UILabel *iLabel;

@property (nonatomic, strong) UISwitch *switchCtl;

@end

@implementation CXHSplashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *itemLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancelClicked:)];
    self.navigationItem.leftBarButtonItem = itemLeft;
    
    [self loadUI];
    [self initShowLabel];
    
    UIButton *btn2 = [UIButton new];
    btn2.backgroundColor = [UIColor orangeColor];
    btn2.layer.cornerRadius = 20;
    
    btn2.frame = CGRectMake((kCXHScreenWidth -150)/2, kCXHTopBarHeight + 100, 150, 40);
    [btn2 setTitle:@"加载开屏广告" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(loadSplashAd:) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat height = kCXHTopBarHeight + 180;
    UILabel *switchLabel  = [[UILabel alloc]initWithFrame:CGRectMake(30, height, 250, 30)];
    switchLabel.text = @"设置跳过按钮右上角:";
    switchLabel.font = [UIFont systemFontOfSize:22];
    switchLabel.textColor = [UIColor blackColor];
    [self.view addSubview:switchLabel];
    
    self.switchCtl = [[UISwitch alloc] init];
    self.switchCtl.on = NO;
    //位置的x,y可以改，但是按钮宽、高不可以改，就算设置了也没效果
    self.switchCtl.frame = CGRectMake(300, height, 80, 40);

    [self.view addSubview:self.switchCtl];
}


- (void)onCancelClicked:(id)sender {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadSplashAd:(UIButton *)sender{
    sender.enabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        sender.enabled = YES;
    });
    if (self.splashAd) {
        return;
    }
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    self.splashAd = [[CXHAdSDKSplashAd alloc]init];
    self.splashAd.delegate = self;
    self.splashAd.showTime = 5;
    self.splashAd.controller = self;
    self.splashAd.setSkipButtonToRightTop = self.switchCtl.on;
    NSString *posid = @"88437632";
    if (self.textField.text.length > 0) {
        posid = self.textField.text;
    }
    self.splashAd.posId = posid;
    // 3、设置默认启动图(一般设置启动图的平铺颜色为背景颜色，使得视觉效果更加平滑)
    self.splashAd.backgroundImage = [UIImage imageNamed:@"750x1334.png"];
    
    // 4、开屏广告机型适配
    CGFloat bottomViewHeight;
    if (kCXHCurveScreen) { // 刘海屏
        bottomViewHeight = [UIScreen mainScreen].bounds.size.height * 0.15;
    } else {
        bottomViewHeight = [UIScreen mainScreen].bounds.size.height - [UIScreen mainScreen].bounds.size.width * (960 / 640.0);
    }
    // 5、底部视图设置，非必选项
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    bottomView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, bottomViewHeight);
    UIImageView *logoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"LOGO.png"]];
    logoImageView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width-135)/2, (bottomViewHeight-46)/2, 135, 46);
    [bottomView addSubview:logoImageView];
    self.splashAd.bottomView = bottomView;
    [self.splashAd loadSplashAd];
//    [self.splashAd loadAndShowSplashAdInWindow:window];
}

#pragma mark - CXHAdSDKSplashAdDelegate
/**
 开屏广告点击回调, 可能会打开 AppStore, WebView, ThirdApp etc.

 @param splashAd 开屏广告
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_splashAdDidClick:(CXHAdSDKSplashAd *)splashAd loadingPageURL:(NSString *)loadingPageURL{
    
}

/**
 开屏广告关闭回调, 触发条件: 点击广告, 点击跳过, 超时关闭

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidClose:(CXHAdSDKSplashAd *)splashAd{
    self.splashAd = nil;
}

/**
 开屏广告展示回调

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdExposured:(CXHAdSDKSplashAd *)splashAd{
    
}

/**
 开屏广告加载成功
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidLoad:(CXHAdSDKSplashAd *)splashAd{
    if ([self.splashAd isAdValid]) {
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        [self.splashAd showSplashAdInWindow:window];
    }
}

/**
 开屏广告加载失败
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidFailToLoad:(CXHAdSDKSplashAd *)splashAd error:(NSError *)error{
    
}

/**
 *  开屏广告成功展示
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdSuccessPresentScreen:(CXHAdSDKSplashAd *)splashAd{
    
}

/**
 *  开屏广告展示失败
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdFailToPresent:(CXHAdSDKSplashAd *)splashAd withError:(NSError *)error{
    
}

/**
 *  开屏广告关闭落地页
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdCloseLandingPage:(CXHAdSDKSplashAd *)splashAd{
    
}

- (void)loadUI{
    CGFloat height = kCXHTopBarHeight + 20;
    UILabel *appidLabel  = [[UILabel alloc]initWithFrame:CGRectMake(30, height, 80, 30)];
    appidLabel.text = @"posid:";
    appidLabel.font = [UIFont systemFontOfSize:22];
    appidLabel.textColor = [UIColor blackColor];
    [self.view addSubview:appidLabel];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30 + 80 + 10, height, 200, 30)];
    //通过属性控制是否可以编辑
    textField.enabled = YES;
    textField.delegate = self;
    textField.borderStyle = UITextBorderStyleBezel;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    textField.placeholder = @"88437632";
    textField.textColor = [UIColor colorWithHexString:@"#666666"];
    textField.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:textField];
    
    self.textField = textField;
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    tapGestureRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGestureRecognizer];
}
#pragma mark - UITextFieldDelegate
//是否可以开始编辑状态
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"%s",__func__);
    return YES;
}
  
//已经进入编辑状态
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"%s",__func__);
}
//是否可以结束编辑
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"%s",__func__);
    return YES;
}
//已经结束编辑状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"%s",__func__);
}
//点击右下角return按钮所触发的代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"return");
    //在此回收键盘
    [textField resignFirstResponder];
    return YES;
}
  
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //可以限制文字的改变
    return YES;
}

- (void)hideKeyBoard {
    [self.textField endEditing:YES];
}

- (void)initShowLabel{
    self.iLabel = [[UILabel alloc]initWithFrame:CGRectMake(kCXHScreenWidth/2 - 130, kCXHScreenHeight/2 - 15, 260, 60)];
    self.iLabel.textColor = [UIColor systemOrangeColor];
    self.iLabel.font = [UIFont systemFontOfSize:22];
    self.iLabel.hidden = YES;
    self.iLabel.textAlignment = NSTextAlignmentCenter;
    self.iLabel.numberOfLines = 0;
    [self.view addSubview:self.iLabel];
}

- (void)showText:(NSString *)text {
    self.iLabel.text = text;
    [UIView animateWithDuration:0.25 animations:^{
        self.iLabel.hidden = NO;
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.25 animations:^{
            self.iLabel.hidden = YES;
        }];
    });
}


@end
